/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.hibernate.criteria.field;

import java.util.Arrays;

import com.genia.toolbox.basics.bean.Delimiter;
import com.genia.toolbox.persistence.exception.AggregateException;
import com.genia.toolbox.persistence.hibernate.bean.impl.DefaultQueryable;

/**
 * the default interface of the implementation of <code>Field</code> for the
 * hibernate implementation of the persistence API. It uses
 * <code>Delimiter</code> and sub-fields to generate the HQL queries
 * associated to this field.
 */
public class DefaultHibernateField
    extends DefaultQueryable<HibernateField>
    implements HibernateField
{

  /**
   * is this field an aggregate wether or not the sub-fields are aggregate?
   */
  private transient final boolean forceAggregate;



  /**
   * returns an escaped version of the current Field.
   * 
   * @return an escaped version of the current Field
   * @throws AggregateException
   *           when there is an error with an aggregate definition
   * @see com.genia.toolbox.persistence.hibernate.criteria.field.HibernateField#escapedField()
   */
  public HibernateField escapedField()
      throws AggregateException
  {
    HibernateField[] escapedSubFields = subQueryables.toArray(new HibernateField[subQueryables.size()]);
    for (int i = 0; i < escapedSubFields.length; i++) {
      escapedSubFields[i] = escapedSubFields[i].escapedField();
    }
    return new DefaultHibernateField(forceAggregate, delimiter, escapedSubFields);
  }



  /**
   * Constructor.
   * 
   * @param forceAggregate
   *          iIs this field an aggregate wether or not the sub-fields are
   *          aggregate?
   * @param delimiter
   *          the <code>Delimiter</code> used to assemble the sub fields
   * @param subFields
   *          the sub-Fields
   * @throws AggregateException
   *           when there is an error with an aggregate definition
   */
  public DefaultHibernateField(final boolean forceAggregate, final Delimiter delimiter, final HibernateField... subFields)
      throws AggregateException
  {
    super(delimiter, subFields);
    this.forceAggregate = forceAggregate;
    if (forceAggregate && isAggregate(Arrays.asList(subFields))) {
      throw new AggregateException();
    }
  }



  /**
   * returns <code>true</code> if any of the fields in <code>subFIelds</code>
   * is an aggregate.
   * 
   * @param subFIelds
   *          the fields to check.
   * @return <code>true</code> if any of the fields in <code>subFIelds</code>
   *         is an aggregate
   */
  private boolean isAggregate(final Iterable<HibernateField> subFIelds)
  {
    for (final HibernateField field : subFIelds) {
      if (field.isAggregate()) {
        return true;
      }
    }
    return false;
  }



  /**
   * returns if this <code>Field</code> is an aggregate.
   * 
   * @return <code>true</code> if this <code>Field</code> is an aggregate,
   *         <code>false</code> otherwise.
   */
  public boolean isAggregate()
  {
    return forceAggregate || isAggregate(getSubQueryables());
  }

}
